//
//  NSString+GWURLEncoding.m
//  ShandsMod
//
//  Created by zh_s on 2020/1/2.
//  Copyright © 2020 zh_s. All rights reserved.
//

#import "NSString+GWURLEncoding.h"

@implementation NSString (GWURLEncoding)

// 字符串编码
- (NSString *)urlEncodeString
{
    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(":/?#[]@!$&’(){}<>*+,;="),kCFStringEncodingUTF8));
    return result;
}

// 反URL编码
- (NSString *)decodeFromPercentEscapeString
{
    NSMutableString *outputStr = [NSMutableString stringWithString:self];
    [outputStr replaceOccurrencesOfString:@"+"
                               withString:@" "
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0, [outputStr length])];
    
    return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

@end
